
set more off
set matsize 11000

cd \Data


********************************************************************************
** This code summarizes the premise-level cooling predictions contained in the
** Temp/HH_Cooling_Estimates.dta file. The premise-level estimates will first
** be combined with the premise_characteristics.dta file which contains SMUD
** and Assessor information about each premise.

** This code will recreate the results summarized in Figures 3, 4, A1, A2, A3,
** A9, and A11.
********************************************************************************

use Temp/HH_Cooling_Estimates.dta, clear
sort premise_id
merge 1:1 premise_id using premise_characteristics.dta
keep if _merge == 3
drop _merge

** Keep premises that are zoned for single family residences
keep if (zoning == "R-1" | zoning == "RD 1" | zoning == "RD 2" | zoning == "RD 3" | zoning == "RD 4" | zoning == "RD 5" | zoning == "RD-1" | zoning == "RD-2" | zoning == "RD-3" | zoning == "RD-4" | zoning == "RD-5" | zoning == "RD1" | zoning == "RD2" | zoning == "RD3" | zoning == "RD4" | zoning == "RD5" | zoning == "RD5PD")

** Create premise-level controls for bedrooms (6 bins), square-footage (12 bins),
** and an indicator for multiple stories. Note, premises with missing Assessor
** information will be dropped.

drop if bedrooms_num == 0 | bedrooms_num == .
replace bedrooms_num = 6 if bedrooms_num > 6
drop if Resdnc_sqft == 0 | Resdnc_sqft == .
gen sqft = 0
replace sqft = 1 if Resdnc_sqft < 1000
forvalues i = 2/11 {
	replace sqft = `i' if Resdnc_sqft < 1000 + (`i' - 1) * 150 & Resdnc_sqft >= 1000 + (`i' - 2) * 150
}
replace sqft = 12 if Resdnc_sqft >= 2500
gen multi = 0
replace multi = 1 if stories > 1

** Set the baseline household:

fvset base 1977 year_built
fvset base 3 bedrooms_num
fvset base 6 sqft
fvset base 0 electric_heat
fvset base 1 stories
fvset base 0 multi

save Temp/HH_Cooling_Estimates_Merged.dta, replace

********************************************************************************
** Figure 3 (Top Panel): Average cooling consumption by year-built (without and with controls)
** Note: The point estimates of the conditional average annual cooling by year-built are also displayed in Figures A9 and A11

use Temp/HH_Cooling_Estimates_Merged.dta, clear

reg cooling_kwh i.year_built if electric_heat == 0 & min_temp >=52,  vce(cluster block_group)

forvalues i = 1960/1976 {
	lincom `i'.year_built + _cons
	scalar define cooling_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/2011 {
	lincom `i'.year_built + _cons
	scalar define cooling_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define cooling_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define cooling_year_no = (cooling_y1960)
matrix define se_year_no = (se_y1960)

forvalues i = 1961/2011 {
	matrix define cooling_year_no = (cooling_year_no \ cooling_y`i')
	matrix define se_year_no = (se_year_no \ se_y`i')
}

matrix define cooling_no_control = (cooling_year_no, se_year_no)

reg cooling_kwh i.year_built i.bedrooms##i.sqft##i.multi if electric_heat == 0 & min_temp >=52, vce(cluster block_group)

forvalues i = 1960/1976 {
	lincom `i'.year_built + _cons
	scalar define cooling_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/2011 {
	lincom `i'.year_built + _cons
	scalar define cooling_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define cooling_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define cooling_year = (cooling_y1960)
matrix define se_year = (se_y1960)

forvalues i = 1961/2011 {
	matrix define cooling_year = (cooling_year \ cooling_y`i')
	matrix define se_year = (se_year \ se_y`i')
}

matrix define cooling_with_control = (cooling_year, se_year)

matrix define cooling = (cooling_year_no, cooling_year, se_year)/2

clear
set obs 52
gen year = _n + 1959
svmat cooling
rename cooling1 unconditional /* Unconditional average annual cooling */
rename cooling2 conditional /* Conditional average annual cooling */
rename cooling3 std_err /* Standard errors of conditional average annual cooling */

scatter conditional unconditional year


********************************************************************************
** Figure 3 (Bottom Panel): Average annual consumption by year-built (without and with controls)

use Temp/HH_Cooling_Estimates_Merged.dta, clear

reg total_kwh i.year_built if electric_heat == 0,  vce(cluster block_group)

forvalues i = 1960/1976 {
	lincom `i'.year_built + _cons
	scalar define total_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/2011 {
	lincom `i'.year_built + _cons
	scalar define total_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define total_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define total_year_no = (cooling_y1960)
matrix define se_year_no = (se_y1960)

forvalues i = 1961/2011 {
	matrix define total_year_no = (total_year_no \ total_y`i')
	matrix define se_year_no = (se_year_no \ se_y`i')
}

matrix define total_no_control = (total_year_no, se_year_no)

reg total_kwh i.year_built i.bedrooms##i.sqft##i.multi if electric_heat == 0, vce(cluster block_group)

forvalues i = 1960/1976 {
	lincom `i'.year_built + _cons
	scalar define total_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/2011 {
	lincom `i'.year_built + _cons
	scalar define total_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define total_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define total_year = (total_y1960)
matrix define se_year = (se_y1960)

forvalues i = 1961/2011 {
	matrix define total_year = (total_year \ total_y`i')
	matrix define se_year = (se_year \ se_y`i')
}

matrix define total_with_control = (total_year, se_year)

matrix define total = (total_year_no, total_year, se_year)/2

clear
set obs 52
gen year = _n + 1959
svmat total
rename total1 unconditional /* Unconditional average annual consumption */
rename total2 conditional /* Conditional average annual consumption */
rename cooling3 std_err /* Standard errors of conditional average annual consumption */

scatter conditional unconditional year


********************************************************************************
** Figure 4 (Top Panel): Average cooling consumption by year-built (1968-1989 Vintages Only)

use Temp/HH_Cooling_Estimates_Merged.dta, clear

reg cooling_kwh i.year_built i.bedrooms##i.sqft##i.multi if electric_heat == 0 & min_temp >=52 & year_built>=1968 & year_built<=1989, vce(cluster block_group)

forvalues i = 1968/1976 {
	lincom `i'.year_built + _cons
	scalar define cooling_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/1989 {
	lincom `i'.year_built + _cons
	scalar define cooling_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define cooling_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define cooling_year = (cooling_y1968)
matrix define se_year = (se_y1968)

forvalues i = 1969/1989 {
	matrix define cooling_year = (cooling_year \ cooling_y`i')
	matrix define se_year = (se_year \ se_y`i')
}

matrix define cooling = (cooling_year, se_year)/2

clear
set obs 22
gen year = _n + 1967
svmat cooling
rename cooling1 conditional /* Conditional average annual cooling */
rename cooling2 std_err /* Standard errors of conditional average annual cooling */

scatter conditional year


********************************************************************************
** Figure 4 (Bottom Panel): Average minimumc consumption temperature by year-built (1968-1989 Vintages Only)

use Temp/HH_Cooling_Estimates_Merged.dta, clear

reg min_temp i.year_built i.bedrooms##i.sqft##i.multi if electric_heat == 0 & min_temp >=52 & min_temp <= 72 & year_built>=1968 & year_built<=1989, vce(cluster block_group)

forvalues i = 1968/1976 {
	lincom `i'.year_built + _cons
	scalar define min_temp_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/1989 {
	lincom `i'.year_built + _cons
	scalar define min_temp_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define min_temp_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define min_temp_year = (min_temp_y1968)
matrix define se_year = (se_y1968)

forvalues i = 1969/1989 {
	matrix define min_temp_year = (min_temp_year \ min_temp_y`i')
	matrix define se_year = (se_year \ se_y`i')
}

matrix define min_temp = (min_temp_year, se_year)

clear
set obs 22
gen year = _n + 1967
svmat min_temp_by_year
rename min_temp_by_year1 conditional /* Conditional average minimum consumption temperature */
rename min_temp_by_year2 std_err /* Standard errors of conditional average minimum consumption temperature */

scatter conditional year


********************************************************************************
** Figure A1: Histogram of predicted premise-level minimum consumption temperature

use Temp/HH_Cooling_Estimates_Merged.dta, clear
histogram min_temp if electric_heat == 0 & min_temp >=52 & min_temp <= 72, width(.25) frequency


********************************************************************************
** Figure A2: Average minimum consumption temperature by year-built (without and with controls)

use Temp/HH_Cooling_Estimates_Merged.dta, clear

reg min_temp i.year_built if electric_heat == 0 & min_temp >=52 & min_temp <= 72,  vce(cluster block_group)

forvalues i = 1960/1976 {
	lincom `i'.year_built + _cons
	scalar define min_temp_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/2011 {
	lincom `i'.year_built + _cons
	scalar define min_temp_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define min_temp_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define min_temp_year_no = (min_temp_y1960)
matrix define se_year_no = (se_y1960)

forvalues i = 1961/2011 {
	matrix define min_temp_year_no = (min_temp_year_no \ min_temp_y`i')
	matrix define se_year_no = (se_year_no \ se_y`i')
}

matrix define min_temp_no_control = (min_temp_year_no, se_year_no)

reg min_temp i.year_built i.bedrooms##i.sqft##i.multi if electric_heat == 0 & min_temp >=52 & min_temp <= 72, vce(cluster block_group)

forvalues i = 1960/1976 {
	lincom `i'.year_built + _cons
	scalar define min_temp_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/2011 {
	lincom `i'.year_built + _cons
	scalar define min_temp_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define min_temp_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define min_temp_year = (min_temp_y1960)
matrix define se_year = (se_y1960)

forvalues i = 1961/2011 {
	matrix define min_temp_year = (min_temp_year \ min_temp_y`i')
	matrix define se_year = (se_year \ se_y`i')
}

matrix define min_temp_with_control = (min_temp_year, se_year)

matrix define min_temp_by_year = (min_temp_year_no, min_temp_year, se_year)

clear
set obs 52
gen year = _n + 1959
svmat min_temp_by_year
rename min_temp_by_year1 unconditional /* Unconditional average minimum consumption temperature */
rename min_temp_by_year2 conditional /* Conditional average minimum consumption temperature */
rename min_temp_by_year3 std_err /* Standard errors of conditional average minimum consumption temperature */

scatter conditional unconditional year


********************************************************************************
** Figure A3: Average consumption at minimum consumption temperature by year-built (without and with controls)

use Temp/HH_Cooling_Estimates_Merged.dta, clear

reg min_kwh i.year_built if electric_heat == 0 & min_temp >=52,  vce(cluster block_group)

forvalues i = 1960/1976 {
	lincom `i'.year_built + _cons
	scalar define min_kwh_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/2011 {
	lincom `i'.year_built + _cons
	scalar define min_kwh_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define min_kwh_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define min_kwh_year_no = (min_kwh_y1960)
matrix define se_year_no = (se_y1960)

forvalues i = 1961/2011 {
	matrix define min_kwh_year_no = (min_kwh_year_no \ min_kwh_y`i')
	matrix define se_year_no = (se_year_no \ se_y`i')
}

matrix define min_kwh_no_control = (min_kwh_year_no, se_year_no)

reg min_kwh i.year_built i.bedrooms##i.sqft##i.multi if electric_heat == 0 & min_temp >=52, vce(cluster block_group)

forvalues i = 1960/1976 {
	lincom `i'.year_built + _cons
	scalar define min_kwh_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

forvalues i = 1978/2011 {
	lincom `i'.year_built + _cons
	scalar define min_kwh_y`i' = r(estimate)
	scalar define se_y`i' = r(se)
}

lincom _cons
scalar define min_kwh_y1977 = r(estimate)
scalar define se_y1977 = r(se)

matrix define min_kwh_year = (min_kwh_y1960)
matrix define se_year = (se_y1960)

forvalues i = 1961/2011 {
	matrix define min_kwh_year = (min_kwh_year \ min_kwh_y`i')
	matrix define se_year = (se_year \ se_y`i')
}

matrix define min_kwh_with_control = (min_kwh_year, se_year)

matrix define min_kwh_by_year = (min_kwh_year_no, min_kwh_year, se_year)

clear
set obs 52
gen year = _n + 1959
svmat min_kwh_by_year
rename min_kwh_by_year1 unconditional /* Unconditional average minimum consumption */
rename min_kwh_by_year2 conditional /* Conditional average minimum consumption */
rename min_kwh_by_year3 std_err /* Standard errors of conditional average minimum consumption */

scatter conditional unconditional year



